package com.okyai.qa.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.okyai.common.annotation.Log;
import com.okyai.common.enums.BusinessType;
import com.okyai.qa.domain.QuestionnaireCategory;
import com.okyai.qa.service.IQuestionnaireCategoryService;
import com.okyai.common.core.controller.BaseController;
import com.okyai.common.core.domain.AjaxResult;
import com.okyai.common.utils.poi.ExcelUtil;
import com.okyai.common.utils.ShiroUtils;
import com.okyai.common.utils.StringUtils;
import com.okyai.common.core.domain.Ztree;

/**
 * 问卷分类Controller
 * 
 * @author okyai
 * @date 2021-03-13
 */
@Controller
@RequestMapping("/qa/category")
public class QuestionnaireCategoryController extends BaseController
{
    private String prefix = "qa/category";

    @Autowired
    private IQuestionnaireCategoryService questionnaireCategoryService;

    @RequiresPermissions("qa:category:view")
    @GetMapping()
    public String category()
    {
        return prefix + "/category";
    }

    /**
     * 查询问卷分类树列表
     */
    @RequiresPermissions("qa:category:list")
    @PostMapping("/list")
    @ResponseBody
    public List<QuestionnaireCategory> list(QuestionnaireCategory questionnaireCategory)
    {
    	questionnaireCategory.setTenantId(ShiroUtils.getSysUser().getDeptId());
        List<QuestionnaireCategory> list = questionnaireCategoryService.selectQuestionnaireCategoryList(questionnaireCategory);
        return list;
    }

    /**
     * 导出问卷分类列表
     */
    @RequiresPermissions("qa:category:export")
    @Log(title = "问卷分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(QuestionnaireCategory questionnaireCategory)
    {
        List<QuestionnaireCategory> list = questionnaireCategoryService.selectQuestionnaireCategoryList(questionnaireCategory);
        ExcelUtil<QuestionnaireCategory> util = new ExcelUtil<QuestionnaireCategory>(QuestionnaireCategory.class);
        return util.exportExcel(list, "category");
    }

    /**
     * 新增问卷分类
     */
    @GetMapping(value = { "/add/{categoryId}", "/add/" })
    public String add(@PathVariable(value = "categoryId", required = false) Long categoryId, ModelMap mmap)
    {
        if (StringUtils.isNotNull(categoryId))
        {
            mmap.put("questionnaireCategory", questionnaireCategoryService.selectQuestionnaireCategoryById(categoryId));
        }
        return prefix + "/add";
    }

    /**
     * 新增保存问卷分类
     */
    @RequiresPermissions("qa:category:add")
    @Log(title = "问卷分类", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(QuestionnaireCategory questionnaireCategory)
    {
    	questionnaireCategory.setTenantId(ShiroUtils.getSysUser().getDeptId());
        return toAjax(questionnaireCategoryService.insertQuestionnaireCategory(questionnaireCategory));
    }

    /**
     * 修改问卷分类
     */
    @GetMapping("/edit/{categoryId}")
    public String edit(@PathVariable("categoryId") Long categoryId, ModelMap mmap)
    {
        QuestionnaireCategory questionnaireCategory = questionnaireCategoryService.selectQuestionnaireCategoryById(categoryId);
        mmap.put("questionnaireCategory", questionnaireCategory);
        return prefix + "/edit";
    }

    /**
     * 修改保存问卷分类
     */
    @RequiresPermissions("qa:category:edit")
    @Log(title = "问卷分类", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(QuestionnaireCategory questionnaireCategory)
    {
        return toAjax(questionnaireCategoryService.updateQuestionnaireCategory(questionnaireCategory));
    }

    /**
     * 删除
     */
    @RequiresPermissions("qa:category:remove")
    @Log(title = "问卷分类", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{categoryId}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("categoryId") Long categoryId)
    {
        return toAjax(questionnaireCategoryService.deleteQuestionnaireCategoryById(categoryId));
    }

    /**
     * 选择问卷分类树
     */
    @GetMapping(value = { "/selectCategoryTree/{categoryId}", "/selectCategoryTree/" })
    public String selectCategoryTree(@PathVariable(value = "categoryId", required = false) Long categoryId, ModelMap mmap)
    {
        if (StringUtils.isNotNull(categoryId))
        {
            mmap.put("questionnaireCategory", questionnaireCategoryService.selectQuestionnaireCategoryById(categoryId));
        }
        return prefix + "/tree";
    }

    /**
     * 加载问卷分类树列表
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData()
    {
        List<Ztree> ztrees = questionnaireCategoryService.selectQuestionnaireCategoryTree();
        return ztrees;
    }
}
